package com.meihua.shellapp.activity.set;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.model.Response;
import com.meihua.shellapp.R;
import com.meihua.shellapp.activity.login.LoginActivity;
import com.meihua.shellapp.base.BaseActivity;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.common.NetHelper;
import com.meihua.shellapp.utils.URL;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by YaoBaoHua on 2017/9/27 0027.
 * desc:
 * 修改密码
 */

public class ChangePasswordActivity extends BaseActivity {
    @Bind(R.id.iv_top_left)
    ImageView ivTopLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.et_oldpwd)
    EditText etOldpwd;
    @Bind(R.id.et_newpwd)
    EditText etNewpwd;
    @Bind(R.id.et_confirm_newpwd)
    EditText etConfirmNewpwd;

    @Override
    public int getLayoutId() {
        return R.layout.activity_chang_password;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        ivTopLeft.setVisibility(View.VISIBLE);
        ivTopLeft.setOnClickListener(this);
        tvTitle.setText("修改密码");
        tvTitle.setVisibility(View.VISIBLE);
    }

    @OnClick(R.id.tv_submit)
    public void onViewClicked() {
        final String oldPWD = etOldpwd.getText().toString().trim();
        final String newpwd = etNewpwd.getText().toString().trim();
        final String confirmNewpwd = etConfirmNewpwd.getText().toString().trim();
        if(TextUtils.isEmpty(oldPWD)){
            Toast.makeText(this,"请输入原密码",Toast.LENGTH_SHORT).show();
        }else if(TextUtils.isEmpty(newpwd)){
            Toast.makeText(this,"请输入新密码",Toast.LENGTH_SHORT).show();
        }else if(TextUtils.isEmpty(confirmNewpwd)){
            Toast.makeText(this,"请输入确认密码",Toast.LENGTH_SHORT).show();
        }else if(!newpwd.equals(confirmNewpwd)){
            Toast.makeText(this,"两次密码不一致",Toast.LENGTH_SHORT).show();
        }else {
            Map<String, String> params = new HashMap() {{
                put("m_number", MyApplication.getInstance().getUserId());
                put("oldpwd", oldPWD);
                put("newpwd1", newpwd);
                put("newpwd2", confirmNewpwd);
            }};
            NetHelper.post(URL.getUrl().EDITPWD, params, new NetHelper.HttpCallBack() {
                @Override
                public void onSuccess(Response<String> response) {
                    JSONObject obj = null;
                    try {
                        obj = new JSONObject(response.body());
                        if (obj.getInt("code") == 1) {
                            Toast.makeText(ChangePasswordActivity.this, "修改成功，即将跳转到登录界面", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(ChangePasswordActivity.this, LoginActivity.class));
                            finish();
                        } else {
                            Toast.makeText(ChangePasswordActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, true);
        }

    }
}
